<?php
/**
 * @Author:      zk
 * @DateTime:    18-7-13 上午11:49
 * @Description: 用于维护系统资源相关
 */
class OAResourceController extends BaseController
{
    public function __construct(){
        header('Access-Control-Allow-Origin:*');	//解决资源跨域问题
	header('Access-Control-Allow-Credentials', 'true');
    }
    
    /**
     * @Author:      zk
     * @DateTime:    18-7-14 上午10:09
     * @Description: 用于展示资源的列表页
     * @param string $token             用户token
     * @param int $pid                  父级id 0代表是最上级
     * @link: http://api.anhui.com/oa/resource/index?token=cd42ad78766754e9a663e4ed43b64c02&pid=0
     */
    public function index(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'pid' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get('oa_error.args_error_100000');
                $response['result'] = $validator->messages();
                break;
            }
            $oa_user = $this->verificationOAUserToken(Input::get('token'), $response);
            if(is_null($oa_user)){
                break;
            }
            
            $pid = Input::get('pid', 0);    //父id
            $select_arr = [
                'id', 'url', 'name', 'en_name', 'pid', 'ext_id', 'type', 'status', 'sort', 'icon',
            ];
            $list = OAResource::where('pid', $pid)
                    ->whereIn('status', [1, 2])
                    ->select($select_arr)
                    ->orderBy('sort')
                    ->get();
            $count = $list->count();
            $response = array('code' => 0, 'msg' => 'ok','result' => array('count' => $count, 'list' => $list));
        }while(false);
        Log::info('info', ['system' => 'oa', 'event' => 'resource/index', 'input' => Input::all(), 'output' => $response]);
        return json_encode($response);
    }

    /**
     * @Author:      zk
     * @DateTime:    18-7-13 上午9:40
     * @Description: 添加/修改资源 (目录、菜单、资源)
     * @param string $token             用户token
     * @param string $name              资源名称
     * @param string $en_name           英文名称
     * @param string $pid               默认属于组id 0代表是最上级
     * @param string $type              资源的类型 (1:目录父级;2:菜单;3:资源;)
     * @param string $url               路径URL
     * @param string $icon              图标名称
     * @param int $sort                 排序
     * @param int $id                   资源id
     * @param int $ext_id               OA前端自定义id  [三级操作资源时匹配]
     * @link: http://api.anhui.com/oa/resource/store?token=1ce108359a4965e9305bd39b8692a1af&name=工作台&pid=0&type=1
     */
    public function store(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'name' => 'required',
                'pid' => 'required',
                'type' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get('oa_error.args_error_100000');
                $response['result'] = $validator->messages();
                break;
            }
            $oa_user = $this->verificationOAUserToken(Input::get('token'), $response);
            if(is_null($oa_user)){
                break;
            }
            $user_id    = $oa_user->id;                 //用户id
            $name       = Input::get('name');           //资源名称
            $pid        = Input::get('pid');            //默认属于组id 0代表是最上级
            $type       = Input::get('type');           //资源的类型 (1:目录父级;2:菜单;3:资源;)
            $en_name    = Input::get('en_name', '');    //英文名称
            $url        = Input::get('url', '');        //路径URL
            $icon       = Input::get('icon', '');       //图标名称
            $sort       = Input::get('sort', '');       //排序
            $id         = Input::get('id', '');         //资源id
            $ext_id     = Input::get('ext_id', '');     //前端自定义id
            if($id != ''){
                $resource = OAResource::find($id);
                $oldData = '';
                $resource->updated_at = Common::num2DateTime(time());
                $resource->updated_by = $user_id;
                $typeLog = 1;           //添加
                $descLog = '添加资源';
            } else {
                $resource = new OAResource();
                $oldData = json_encode($response);
                $resource->created_at = Common::num2DateTime(time());
                $resource->created_by = $user_id;
                $resource->status = 1;
                $typeLog = 1;           //修改资源
                $descLog = '修改资源';
            }
            
            $resource->name = $name;
            $resource->pid = $pid;
            $resource->type = $type;
            $resource->en_name = $en_name;
            $resource->url = $url;
            $resource->icon = $icon;
            if($sort != ''){
                $resource->sort = $sort;
            }
            if($ext_id != ''){
                $resource->ext_id = $ext_id;
            }
            $newData = json_encode($response);
            try {
                OAOpLog::writeLog('resource/store', $typeLog, $descLog, $oldData, $newData, $user_id);
            } catch (Exception $e) {
                $response = Lang::get('oa_error.resource_create_error_210012');
                break;
            }
            try {
                $resource->save();
            }catch (Exception $e){
                Log::info('info', ['system' => 'oa', 'event' => 'resource/store', 'input' => Input::all(), 'exception' => $e]);
                $response = Lang::get('oa_error.resource_create_error_210012');
                break;
            }
            $response = array('code' => 0, 'msg' => 'ok');
        }while(false);
        Log::info('info', ['system' => 'oa', 'event' => 'resource/store', 'input' => Input::all(), 'output' => $response]);
        return json_encode($response);
    }

    /**
     * @Author:      zk
     * @DateTime:    18-7-14 上午10:09
     * @Description: 修改资源状态
     * @param string $token             用户token
     * @param int $id                   资源id
     * @param int $status               状态值 (1:开启;2:冻结;3:删除;)
     * @link: http://api.anhui.com/oa/resource/status?token=1ce108359a4965e9305bd39b8692a1af&id=1&status=2
     */
    public function status(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required',
                'status' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get('oa_error.args_error_100000');
                $response['result'] = $validator->messages();
                break;
            }
            $oa_user = $this->verificationOAUserToken(Input::get('token'), $response);
            if(is_null($oa_user)){
                break;
            }
            $user_id    = $oa_user->id;                 //用户id
            $id         = Input::get('id');
            $status     = Input::get('status');
            $statusArray = [
                1 => '开启资源',
                2 => '冻结资源',
                3 => '删除资源',
            ];
            $oldData = json_encode(OAResource::where('id', $id)->first());
            try {
                $update_data = [
                    'status' => $status,
                    'updated_at' => Common::num2DateTime(time()),
                    'updated_by' => $user_id,
                ];
                OAResource::where('id', $id)->update($update_data);
            }catch (Exception $e){
                Log::info('info', ['system' => 'oa', 'event' => 'resource/status', 'input' => Input::all(), 'exception' => $e]);
                $response = Lang::get('oa_error.resource_update_status_error_210013');
                break;
            }
            $newData = json_encode(OAResource::where('id', $id)->first());
            try {
                OAOpLog::writeLog('resource/status', 2, $statusArray[$status], $oldData, $newData, $user_id);
            } catch (Exception $e) {
                $response = Lang::get('oa_error.resource_update_status_error_210013');
                break;
            }
            
            $response = array('code' => 0, 'msg' => 'ok');
        }while(false);
        Log::info('info', ['system' => 'oa', 'event' => 'resource/status', 'input' => Input::all(), 'output' => $response]);
        return json_encode($response);
    }

    /**
     * @Author:      zk
     * @DateTime:    18-7-13 上午11:45
     * @Description: 资源详情
     * @param string $token             用户token
     * @param int $id                   资源id
     * @link: http://api.anhui.com/oa/resource/show?token=28859210b692656c3af38e265e93635b&id=1
     */
    public function show(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get('oa_error.args_error_100000');
                $response['result'] = $validator->messages();
                break;
            }
            $oa_user = $this->verificationOAUserToken(Input::get('token'), $response);
            if(is_null($oa_user)){
                break;
            }
            $resource = OAResource::find(Input::get('id'));
            $response = array('code' => 0, 'msg' => 'ok','result' => $resource);
        }while(false);
        Log::info('info', ['system' => 'oa', 'event' => 'resource/show', 'input' => Input::all(), 'output' => $response]);
        return json_encode($response);
    }
    
    /**
     * @Author:      zk
     * @DateTime:    18-7-16 下午4:45
     * @Description: 获取所有资源里列表
     * @param string $token             用户token
     * @link: http://api.anhui.com/oa/resource/allResource?token=cbaeef3465cf329e7167fedb847fb11b
     */
    public function allResource(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get('oa_error.args_error_100000');
                $response['result'] = $validator->messages();
                break;
            }
            $oa_user = $this->verificationOAUserToken(Input::get('token'), $response);
            if(is_null($oa_user)){
                break;
            }
            
            //循环查库  1
            $status_arr = [1];
            $res_type = 2;
            $oar_1 = OAResource::getResourceList(1, $status_arr, $res_type);
            foreach ($oar_1 as &$v_1) {
                $oar_2 = OAResource::getResourceList(2, $status_arr, $res_type, $v_1->id);
                $v_1->child_list = $oar_2;
                foreach ($oar_2 as &$v_2) {
                    $oar_3 = OAResource::getResourceList(3, $status_arr, $res_type, $v_2->id);
                    $v_2->child_list = $oar_3;
                }
            }
            $response = array('code' => 0, 'msg' => 'ok','result' => $oar_1);
            
            //循环数据  2
//            $status_arr = [1];
//            $res_type = 1;
//            $oar_1 = OAResource::getResourceList(1, $status_arr, $res_type);
//            $oar_2 = OAResource::getResourceList(2, $status_arr, $res_type);
//            $oar_3 = OAResource::getResourceList(3, $status_arr, $res_type);
//            $level_2 = [];
//            foreach ($oar_2 as $v_1) {
//                $v_1['child_list'] = [];
//                foreach ($oar_3 as $v_2) {
//                    if($v_1['id'] == $v_2['pid']){
//                        $v_1['child_list'][] = $v_2;
//                        unset($v_2);
//                    }
//                }
//                $level_2[] = $v_1;
//            }
//            $level_1 = [];
//            foreach ($oar_1 as $v_1) {
//                $v_1['child_list'] = [];
//                foreach ($level_2 as $v_2) {
//                    if($v_1['id'] == $v_2['pid']){
//                        $v_1['child_list'][] = $v_2;
//                        unset($v_2);
//                    }
//                }
//                $level_1[] = $v_1;
//            }
//            $response = array('code' => 0, 'msg' => 'ok','result' => $level_1);
            
        }while(false);
        Log::info('info', ['system' => 'oa', 'event' => 'resource/allResource', 'input' => Input::all(), 'output' => $response]);
        return json_encode($response);
    }

}